*******************
***** Table 7 *****
*******************
global Section4 "D:\Dropbox\unequal_gains\QJE revision plan\analysis\section4_data"
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"

** Panel A **

use "$Section4/analysis_main_state.dta", clear

* Set up table
gen Description=" "
replace Description="Beta" if [_n]==1
replace Description="SE" if [_n]==2
replace Description="First Stage F" if [_n]==3
* note first stage F stat will be adjusted for clustering across product modules
replace Description="Cragg-Donald F" if [_n]==4
replace Description="N" if [_n]==5

* Col (1) first stage
reg lRMS_total_spending_QM_2006 inst_raw_lvl [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
gen Col_1=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_1=`b' if Description=="Beta"
replace Col_1=`se' if Description=="SE"
replace Col_1=e(N) if Description=="N"

* Col (2) OLS continued prod. infl. reduced form
reg RMS_lces_price_index_win inst_raw_lvl [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
gen Col_2=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_2=`b' if Description=="Beta"
replace Col_2=`se' if Description=="SE"
replace Col_2=e(N) if Description=="N"

* Col (3) IV continued prod. infl
ivreg2 RMS_lces_price_index_win (lRMS_total_spending_QM_2006 = inst_raw_lvl) [aw=sqrt(RMS_avg_spending)] ///
, first cluster(product_module_code)
gen Col_3=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_3=`b' if Description=="Beta"
replace Col_3=`se' if Description=="SE"
replace Col_3=e(rkf) if Description=="First Stage F"
replace Col_3=e(cdf) if Description=="Cragg-Donald F"
replace Col_3=e(N) if Description=="N"

* Col (4) same as (3) but clustering by state
ivreg2 RMS_lces_price_index_win (lRMS_total_spending_QM_2006 = inst_raw_lvl) [aw=sqrt(RMS_avg_spending)] ///
, first cluster(fips)
gen Col_4=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_4=`b' if Description=="Beta"
replace Col_4=`se' if Description=="SE"
replace Col_4=e(rkf) if Description=="First Stage F"
replace Col_4=e(cdf) if Description=="Cragg-Donald F"
replace Col_4=e(N) if Description=="N"

* Col (5) OLS full infl. reduced form
reg RMS_lfullces_pi_win_common inst_raw_lvl [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
gen Col_5=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_5=`b' if Description=="Beta"
replace Col_5=`se' if Description=="SE"
replace Col_5=e(N) if Description=="N"

* Col (6) IV full infl
ivreg2 RMS_lfullces_pi_win_common (lRMS_total_spending_QM_2006 = inst_raw_lvl) [aw=sqrt(RMS_avg_spending)] ///
, first cluster(product_module_code)
gen Col_6=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_6=`b' if Description=="Beta"
replace Col_6=`se' if Description=="SE"
replace Col_6=e(rkf) if Description=="First Stage F"
replace Col_6=e(cdf) if Description=="Cragg-Donald F"
replace Col_6=e(N) if Description=="N"

* Col (7) same as (6) but clustering by state
ivreg2 RMS_lfullces_pi_win_common (lRMS_total_spending_QM_2006 = inst_raw_lvl) [aw=sqrt(RMS_avg_spending)] ///
, first cluster(fips)
gen Col_7=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_7=`b' if Description=="Beta"
replace Col_7=`se' if Description=="SE"
replace Col_7=e(rkf) if Description=="First Stage F"
replace Col_7=e(cdf) if Description=="Cragg-Donald F"
replace Col_7=e(N) if Description=="N"

* Finalize table and save
order Descr Col*
keep Descr Col*
format Col* %15.8f
drop if [_n]>5
save "$resultspath\Table7a", replace

** Panel B **
use "$Section4/analysis_main_state.dta", clear

* Set up table
gen Description=" "
replace Description="Beta" if [_n]==1
replace Description="SE" if [_n]==2
replace Description="First Stage F" if [_n]==3
* note first stage F stat will be adjusted for clustering across product modules
replace Description="Cragg-Donald F" if [_n]==4
replace Description="N" if [_n]==5
replace Description="Beta_2" if [_n]==6
replace Description="SE_2" if [_n]==7

* Col. (1) continued infl with location f.e.
ivreg2 RMS_lces_price_index_win (lRMS_total_spending_QM_2006=inst_raw_lvl) i.fips [aw=sqrt(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_1=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_1=`b' if Description=="Beta"
replace Col_1=`se' if Description=="SE"
replace Col_1=e(rkf) if Description=="First Stage F"
replace Col_1=e(cdf) if Description=="Cragg-Donald F"
replace Col_1=e(N) if Description=="N"

* Col. (2) growth specification for continued infl
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending=inst_linageres_w) i.fips [aw=sqrt(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_2=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_2=`b' if Description=="Beta"
replace Col_2=`se' if Description=="SE"
replace Col_2=e(rkf) if Description=="First Stage F"
replace Col_2=e(cdf) if Description=="Cragg-Donald F"
replace Col_2=e(N) if Description=="N"

* Col. (3) horse race for continued infl
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending lRMS_total_spending_QM_2006 = inst_raw_lvl inst_allres_w) ///
i.quality_rank i.product_group_code i.fips [aw=sqrt(RMS_avg_spending)], ///
partial(i.quality_rank i.product_group_code i.fips) first cluster(product_module_code)
gen Col_3=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_3=`b' if Description=="Beta"
replace Col_3=`se' if Description=="SE"
replace Col_3=e(rkf) if Description=="First Stage F"
replace Col_3=e(cdf) if Description=="Cragg-Donald F"
replace Col_3=e(N) if Description=="N"
local b2: di %6.5f `=b[1,2]'
replace Col_3=`b2' if Description=="Beta_2"
matrix sd2 = sqrt(var[2,2])
local se2: di %6.5f `=sd2[1,1]'
replace Col_3=`se2' if Description=="SE_2"

* Col. (4) full infl with location f.e.
ivreg2 RMS_lfullces_pi_win_common (lRMS_total_spending_QM_2006=inst_raw_lvl) i.fips [aw=sqrt(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_4=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_4=`b' if Description=="Beta"
replace Col_4=`se' if Description=="SE"
replace Col_4=e(rkf) if Description=="First Stage F"
replace Col_4=e(cdf) if Description=="Cragg-Donald F"
replace Col_4=e(N) if Description=="N"

* Col. (5) growth specification for full infl
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending=inst_linageres_w) i.fips [aw=sqrt(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_5=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_5=`b' if Description=="Beta"
replace Col_5=`se' if Description=="SE"
replace Col_5=e(rkf) if Description=="First Stage F"
replace Col_5=e(cdf) if Description=="Cragg-Donald F"
replace Col_5=e(N) if Description=="N"

* Col. (6) horse race for full infl
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending lRMS_total_spending_QM_2006 = inst_raw_lvl inst_allres_w) ///
i.quality_rank i.product_group_code i.fips [aw=sqrt(RMS_avg_spending)], ///
partial(i.quality_rank i.product_group_code i.fips) first cluster(product_module_code)
gen Col_6=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_6=`b' if Description=="Beta"
replace Col_6=`se' if Description=="SE"
replace Col_6=e(rkf) if Description=="First Stage F"
replace Col_6=e(cdf) if Description=="Cragg-Donald F"
replace Col_6=e(N) if Description=="N"
local b2: di %6.5f `=b[1,2]'
replace Col_6=`b2' if Description=="Beta_2"
matrix sd2 = sqrt(var[2,2])
local se2: di %6.5f `=sd2[1,1]'
replace Col_6=`se2' if Description=="SE_2"

* Finalize table and save
order Descr Col*
keep Descr Col*
format Col* %15.8f
drop if [_n]>7
save "$resultspath\Table7b", replace
